﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentValidation;
using RedeSocial.ViewModels.Interfaces;
using RedeSocial.ViewModels;
using RedeSocial.ViewModels.Enums;

namespace RedeSocial.Validation
{
	/// <summary>
	/// Class responsible for validating the login view.
	/// </summary>
	public class CreateUserValidator : AbstractValidator<ICreateUserViewModel>
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="LoginValidator"/> class.
		/// </summary>
		/// <param name="languageId">The language id to be use when getting the messages.</param>
		public CreateUserValidator(int languageId)
			: base()
		{
			RuleFor(user => user.Name).NotEmpty().WithMessage(ResourceManager.GetResource(Constants.CREATEUSERVIEW_INVALIDNAMEVALIDATION, languageId));
			RuleFor(user => user.Email).NotEmpty().When(user => string.IsNullOrEmpty(user.Email)).WithMessage(ResourceManager.GetResource(Constants.CREATEUSERVIEW_INVALIDEMAILVALIDATION, languageId));
			RuleFor(user => user.Email).EmailAddress().When(user => !string.IsNullOrEmpty(user.Email)).WithMessage(ResourceManager.GetResource(Constants.CREATEUSERVIEW_INVALIDEMAILVALIDATION, languageId));
			RuleFor(user => user.Gender).Must(gender => gender != Gender.Unknown).WithMessage(ResourceManager.GetResource(Constants.CREATEUSERVIEW_INVALIDGENDERVALIDATION, languageId));
			RuleFor(user => user.Birthday).Must(birthday => (birthday.HasValue && birthday.Value.Date.AddYears(10) <= DateTime.Now.Date)).WithMessage(ResourceManager.GetResource(Constants.CREATEUSERVIEW_INVALIDBIRTHDAYVALIDATION, languageId));
			RuleFor(user => user.Password).Length(5, 24).When(user => user.IsNew).WithMessage(ResourceManager.GetResource(Constants.CREATEUSERVIEW_INVALIDPASSWORDVALIDATION, languageId));
			RuleFor(user => user.ConfirmPassword).Equal(user => user.Password).When(user => user.IsNew || !string.IsNullOrEmpty(user.Password)).WithMessage(ResourceManager.GetResource(Constants.CREATEUSERVIEW_INVALIDCONFIRMPASSWORDVALIDATION, languageId));
			RuleFor(user => user.Country).NotNull().WithMessage(ResourceManager.GetResource(Constants.CREATEUSERVIEW_INVALIDCOUNTRYVALIDATION, languageId));
			RuleFor(user => user.AgreeWithTOS).Equal(true).WithMessage(ResourceManager.GetResource(Constants.CREATEUSERVIEW_INVALIDAGREEWITHTERMSVALIDATION, languageId));
		}

	}
}
